package com.wangsh.projDemo.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 二维码工具类
 * 
 * @author Administrator
 *
 */
public class QRcodeUtil
{

	/**
	 * 生成二维码方法
	 * 
	 * @param content 输入一个字符串
	 * @return BufferedImage 返回的是二维码图片的流
	 */
	public static  BufferedImage operGetEncode(String content) throws Exception
	{
		// 设置图像宽度
		int width = 152;
		// 设置图像高度
		int height = 152;
		// 设置图像类型
		String format = "png";
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		// 设置编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		// 设置二维码级别
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		hints.put(EncodeHintType.MARGIN, 0);
		// 1、读取文件转换为字节数组
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		// ByteArrayOutputStream out = new ByteArrayOutputStream();
		// BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		//toBufferedImage：image流数据处理
		BufferedImage image = toBufferedImage(bitMatrix);
		//String path = "D:" + "AAA1" + ".png";
		// Path path1 = Paths.get( path);
		// 输出图像
		// MatrixToImageWriter.writeToPath(bitMatrix, format, path1);
		//ImageIO.write(image, "PNG", new File(path)) ;
		return image;

	}

	/**
	 * image流数据处理
	 *
	 * @author ianly
	 */
	public static BufferedImage toBufferedImage(BitMatrix matrix)
	{
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++)
		{
			for (int y = 0; y < height; y++)
			{
				image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		return image;

	}

	/**
	 * 解析二维码方法
	 * 
	 * @param content 输入一个路径 这个路径是 二维码所在的路径+二维码的全称(包含二维码图片的类型 如 ".png") 返回一个字符串
	 * @return 返回的是输入的字符串
	 */
	public static String operAnalyzeEncode(String path)
	{
		String content = null;
		BufferedImage image;
		 
			if(path==null)
			{
				 content="http://45.35.10.100:10001/link/facebook.htm";
				 return content;
			}else {
				try
				{
					image = ImageIO.read(new File(path));
					LuminanceSource source = new BufferedImageLuminanceSource(image);
					Binarizer binarizer = new HybridBinarizer(source);
					BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
					Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
					hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
					Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
					content = result.getText();
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				return content;
			}
			 
	}

}
